import PyPDF2

try:
    fname = input('Please enter a file name, default(pythonlearn.pdf)\n')
    if fname == '':
        fname = 'pythonlearn.pdf'
    print('beginning load file...')
    reader = PyPDF2.PdfReader(fname)
except Exception as e:
    print('can\'t open the file ->', fname)
    exit()

counter = {}

print('begin handler it...')
for page in reader.pages:
    text = page.extract_text()

    lines = text.splitlines()

    for line in lines:
        words = line.lower().strip().split()

        for word in words:
            for character in word:
                if not (character >= 'a' and character <= 'z'):
                    continue
                counter[character] = counter.get(character, 0) + 1

t = [(c, k) for k, c in counter.items()]
total_count = 0
for count, _ in t:
    total_count += count

t.sort(reverse=True)
for count, character in t:
    first = '{:<5}'.format(count)
    print(f'{first} {character} {round(count / total_count * 100, 2)}')

print(total_count)